<template>
    <Card :bordered="false" dis-hover>
        <jg-Search
            :searchData="searchData"
            @on-enter-input="askDatasearch"
            @on-clear-input="askDatasearch"
            @on-change-change="askDatanow"
        ></jg-Search>
        <div class="i-table-no-border">
            <Table
                highlight-row
                ref="table"
                :columns="columns"
                :loading="loading"
                :data="data"
                size="small"
                @on-sort-change="sortchange"
            >
                <template slot-scope="{ row }" slot="info">
                    <a @click="handleDetailPatient(row)"
                        >{{ row.user_name }} {{ row.gender_str }}
                        {{ row.age }}岁</a
                    >
                </template>
                <template slot-scope="{ row }" slot="label">
                    <Poptip
                        word-wrap
                        trigger="hover"
                        placement="right"
                        width="400"
                    >
                        <template v-for="(item, index) in row.tags_name">
                            <Tag v-show="index<3" :color="item.type==1?'warning':'primary'" :key="index">{{ item.tags_name.replace(/[\r\n]/g, "") }}</Tag>
                        </template>
                        <div slot="content">
                            <template v-for="(item, index) in row.tags_name">
                                <Tag :color="item.type==1?'warning':'primary'" :key="index">{{ item.tags_name.replace(/[\r\n]/g, "") }}</Tag>
                            </template>
                        </div>
                    </Poptip>
                </template>
                <template slot-scope="{ row }" slot="action">
                    <a
                        v-hasC="{
                            name: 'v1-patient-index-restore-delete',
                            flag: 'v1/patient/index/restore-delete',
                        }"
                        @click="deleteedit({ id: row.id }, text)"
                        >恢复</a
                    >
                </template>
            </Table>
            <div class="ivu-mt ivu-text-center">
                <Page
                    show-total
                    show-elevator
                    show-sizer
                    :total="total"
                    :current.sync="params.page"
                    :page-size="params.pageSize"
                    :page-size-opts="pageOpts"
                    @on-change="changepage"
                    @on-page-size-change="changepageSize"
                />
            </div>
        </div>
        <patient-detail
            ref="patientDetail"
            :btnShow="btnShow"
            @cancelDrawer="askData"
        />
    </Card>
</template>
<script>
import jgSearch from "@/components/search";
import patientDetail from "@/components/patientDetail";
import mixins from "@/mixins/mixins";
import {
    patientindexdeletelist,
    patientindexrestoredelete,
} from "@/api/hqapi/indexone.js";
export default {
    components: { jgSearch, patientDetail },
    mixins: [mixins],
    name: "recycle",
    data() {
        return {
            btnShow: [
                {
                    name: "patient-index-sub5",
                    flag: "v1/patient/index/sub",
                },
                {
                    name: "patient-index-edit-remark5",
                    flag: "v1/patient/index/edit-remark",
                },
                {
                    name: "v1-patient-index-update",
                    flag: "v1/patient/index/update",
                },
                {
                    name: "patient-callback-callback-common-list4",
                    flag: "v1/patient/callback/callback-common-list",
                },
                {
                    name: "patient-index-edit-tags10",
                    flag: "v1/patient/index/edit-tags",
                },
                {
                    name: "patient-index-edit-tags13",
                    flag: "v1/patient/index/edit-tags",
                },
            ],
            text: {
                title: "恢复确认",
                content: "恢复后有可能产生重复数据",
                success: "恢复成功",
            },
            searchData: {
                project: [
                    {
                        id: 0,
                        key: "DatePicker",
                        style: { width: "200px" },
                        value: ["", ""],
                        type: "daterange",
                        options: {
                            disabledDate(date) {
                                return (
                                    date &&
                                    date.valueOf() > Date.now() - 3600 * 1000
                                );
                            },
                        },
                        callback: "change",
                        placeholder: "请选择创建日期",
                    },
                    {
                        id: 1,
                        key: "DatePicker",
                        style: { width: "200px" },
                        value: ["", ""],
                        type: "daterange",
                        options: {
                            disabledDate(date) {
                                return (
                                    date &&
                                    date.valueOf() > Date.now() - 3600 * 1000
                                );
                            },
                        },
                        callback: "change",
                        placeholder: "请选择删除日期",
                    },
                    {
                        id: 2,
                        key: "Input",
                        style: { width: "260px" },
                        value: "",
                        placeholder: "请输入患者姓名/手机号/微信号",
                        clearable: true,
                        callback: "input",
                    },
                    {
                        id: 3,
                        key: "TreeSelect",
                        style: { minWidth: "100px" },
                        placeholder: "病种",
                        value: "",
                        childList: [],
                        clearable: true,
                        callback: "change",
                    },
                ],
            },
            columns: [
                {
                    title: "基本信息",
                    key: "basic",
                    slot: "info",
                    width: 155,
                    fixed: "left",
                },
                // {
                //     title: '微信号',
                //     key: 'wx',
                //     minWidth: 108
                // },
                {
                    title: "归属人",
                    key: "realname",
                    width: 110,
                },
                {
                    title: "回访次数",
                    key: "callback_count",
                    sortable: "custom",
                    width: 120,
                },
                {
                    title: "患者标签",
                    key: "label",
                    slot: "label",
                    width: 220,
                },
                {
                    title: "病种",
                    key: "diseases_name",
                    width: 100,
                },
                {
                    title: "创建时间",
                    key: "created_at_str",
                    width: 155,
                },
                {
                    title: "删除时间",
                    key: "delete_time_str",
                    width: 155,
                },
                {
                    title: "备注",
                    minWidth: 250,
                    key: "remark",
                    ellipsis: true,
                    tooltip: true,
                },
                {
                    title: "操作人",
                    width: 90,
                    key: "delete_realname",
                },
                {
                    title: "操作",
                    width: 90,
                    slot: "action",
                    fixed: "right",
                },
            ],
            data: [],
            params: {
                page: 1,
                pageSize: 10,
                created_stime: "",
                created_etime: "",
                delete_stime: "",
                delete_etime: "",
                keywords: "",
                patient_diseases_id: "",
                sort_callback_count: "",
            },
        };
    },
    methods: {
        /* @函数功能: 初始化 */
        initial() {
            this.storage.set('recycle',["",""] )
            this.storage.set('recycle1',["",""] )
            this.funaskData = patientindexdeletelist;
            this.fundelete = patientindexrestoredelete;
            this.askData();
        },
        askDatas(res) {
            this.data = res.list;
            this.total = res.total;
            this.searchData.project.forEach((item) => {
                if (item.id === 3) {
                    if (item.childList.length === 0) {
                        item.childList = res.condition.diseases;
                    }
                }
            });
        },
        searchparams() {
            this.params.page = 1;
            this.params.sort_callback_count = 0;
            this.searchData.project.forEach((item) => {
                if (item.id === 0) {
                    let keywording = ''
                      this.searchData.project.forEach(items=>{
                        if (items.id === 2) {
                            keywording = items.value;
                        }
                    })
                    if (keywording !== '') {
                        if (JSON.stringify(item.value) !== JSON.stringify(["", ""])) {
                            let timer = [this.$Date(item.value[0]).format('YYYY-MM-DD'),this.$Date(item.value[1]).format('YYYY-MM-DD')]
                            this.storage.set('recycle1',timer )
                            item.value = ["", ""];
                            this.params.created_stime = ''
                            this.params.created_etime = ''
                        }
                    } else {
                        if (JSON.stringify(this.storage.get('recycle1'))!== JSON.stringify(["", ""])) {
                            item.value = this.storage.get('recycle1')
                            this.params.created_stime = item.value[0]
                            this.params.created_etime = item.value[1]
                        }
                    }
                }else if (item.id === 1) {
                    let keywording = ''
                      this.searchData.project.forEach(items=>{
                        if (items.id === 2) {
                            keywording = items.value;
                        }
                    })
                    if (keywording !== '') {
                        if (JSON.stringify(item.value) !== JSON.stringify(["", ""])) {
                            let timer = [this.$Date(item.value[0]).format('YYYY-MM-DD'),this.$Date(item.value[1]).format('YYYY-MM-DD')]
                            this.storage.set('recycle',timer )
                            item.value = ["", ""];
                            this.params.delete_stime = ''
                            this.params.delete_etime = ''
                        }
                    } else {
                        if (JSON.stringify(this.storage.get('recycle'))!== JSON.stringify(["", ""])) {
                            item.value = this.storage.get('recycle')
                            this.params.delete_stime = item.value[0]
                            this.params.delete_etime = item.value[1]
                        }
                    }
                } else if (item.id === 2) {
                    this.params.keywords = item.value;
                } else if (item.id === 3) {
                    item.value = "";
                    this.params.patient_diseases_id = "";
                }
            });
        },
        askDatanow() {
            this.params.page = 1;
            this.searchData.project.forEach((item) => {
                if (item.id === 0) {
                    this.params.created_stime = item.value[0];
                    this.params.created_etime = item.value[1];
                    let timer = ["",""]
                    if (JSON.stringify(item.value) !== JSON.stringify(["", ""])){
                        timer = [this.$Date(item.value[0]).format('YYYY-MM-DD'),this.$Date(item.value[1]).format('YYYY-MM-DD')]
                    }
                    this.storage.set('recycle1',timer )
                }else if (item.id === 1) {
                    this.params.delete_stime = item.value[0];
                    this.params.delete_etime = item.value[1];
                    let timer = ["",""]
                    if (JSON.stringify(item.value) !== JSON.stringify(["", ""])){
                        timer = [this.$Date(item.value[0]).format('YYYY-MM-DD'),this.$Date(item.value[1]).format('YYYY-MM-DD')]
                    }
                    this.storage.set('recycle',timer )
                } else if (item.id === 2) {
                    this.params.keywords = item.value;
                } else if (item.id === 3) {
                    this.params.patient_diseases_id = item.value;
                }
            });
            this.askData();
        },
        handleDetailPatient(row) {
            this.$refs.patientDetail.handleOpen(row);
        },
        sortchange(e) {
            if (e.order === "desc") {
                this.params.sort_callback_count = 2;
            } else if (e.order === "asc") {
                this.params.sort_callback_count = 1;
            } else {
                this.params.sort_callback_count = 0;
            }
            this.askData();
        },
    },
};
</script>
